package com.xjrsoft.module.customer.erp_model.eRP_StockInventory.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.page.ConventPage;
import java.util.List;
import com.xjrsoft.common.utils.DataTransUtil;
import com.xjrsoft.common.result.Response;
import com.xjrsoft.core.tool.utils.BeanUtil;
import com.xjrsoft.module.base.service.IXjrBaseCodeRuleService;
import com.xjrsoft.module.customer.erp_model.eRP_StockInventory.dto.ErpStockinventoryDto;
import com.xjrsoft.module.customer.erp_model.eRP_StockInventory.dto.SaveErpStockinventoryFormDataDto;
import com.xjrsoft.module.customer.erp_model.eRP_StockInventory.vo.ErpStockinventoryFormDataVo;
import com.xjrsoft.module.customer.erp_model.eRP_StockInventory.vo.ErpStockinventoryListVo;
import com.xjrsoft.module.customer.erp_model.eRP_StockInventory.dto.ErpStockinventoryListDto;
import com.xjrsoft.module.customer.erp_model.eRP_StockInventory.vo.ErpStockinventoryentryVo;
import com.xjrsoft.module.customer.erp_model.eRP_StockInventory.entity.ErpStockinventoryentry;
import com.xjrsoft.module.customer.erp_model.eRP_StockInventory.dto.ErpStockinventoryentryDto;
import com.xjrsoft.core.secure.utils.SecureUtil;
import com.xjrsoft.core.tool.utils.FlowableUtil;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.File;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.xjrsoft.core.tool.utils.CollectionUtil;
import com.xjrsoft.core.tool.utils.IoUtil;
import com.xjrsoft.module.excel.entity.XjrExcelImport;
import com.xjrsoft.module.excel.factory.ParserFactory;
import com.xjrsoft.module.excel.parser.ImportDataParser;
import com.xjrsoft.module.excel.service.IXjrExcelImportService;
import org.springframework.web.multipart.MultipartFile;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;

import com.xjrsoft.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;
import com.xjrsoft.module.customer.erp_model.eRP_StockInventory.entity.ErpStockinventory;
import com.xjrsoft.module.customer.erp_model.eRP_StockInventory.vo.ErpStockinventoryVo;
import com.xjrsoft.module.customer.erp_model.eRP_StockInventory.service.IErpStockinventoryService;

/**
 *  控制器
 *
 * @author pp
 * @since 2021-10-08
 */
@RestController
@AllArgsConstructor
@RequestMapping("/erp_stockinventory")
@Api(value = "盘点计划", tags = "盘点计划接口")
public class ErpStockinventoryController {

	private final IXjrExcelImportService importService;

	private final ObjectMapper objectMapper;

	private final IErpStockinventoryService erpStockinventoryService;

	private final IXjrBaseCodeRuleService codeRuleService;

	/**
	 * 详情
	 */
	@GetMapping("/{id}")
	@ApiOperation(value = "详情", notes = "传入erpStockinventory")
	public Response<ErpStockinventoryFormDataVo> getFormData(@PathVariable String id) {
		ErpStockinventoryFormDataVo formDataVo = new ErpStockinventoryFormDataVo();
		// 主表数据
		ErpStockinventory erpStockinventory = erpStockinventoryService.getById(id);
		formDataVo.setErpStockinventoryVo(BeanUtil.copy(erpStockinventory, ErpStockinventoryVo.class));
		// 从表数据
		formDataVo.setErpStockinventoryentryVoList(erpStockinventoryService.getInfo(id));
		return Response.ok(formDataVo);
	}

	/**
	 * 自定义分页 
	 */
	@GetMapping
	@ApiOperation(value = "分页", notes = "传入erpStockinventory")
	public Response<PageOutput<ErpStockinventoryListVo>> getPageList(ErpStockinventoryListDto listDto) {
		IPage<ErpStockinventory> page = erpStockinventoryService.getPageList(listDto);
		List<ErpStockinventoryListVo> records = BeanUtil.copyList(page.getRecords(), ErpStockinventoryListVo.class);
		// 转换列表数据
		DataTransUtil.transListShowData(records);
		return Response.ok(ConventPage.getPageOutput(page.getTotal(), records));
	}

	/**
	 * 新增 
	 */
	@PostMapping
	@ApiOperation(value = "新增", notes = "传入erpStockinventory")
	public Response save(@RequestBody SaveErpStockinventoryFormDataDto formDto) {
		ErpStockinventoryDto erpStockinventoryDto = formDto.getErpStockinventoryDto();
		ErpStockinventory erpStockinventory = BeanUtil.copy(erpStockinventoryDto, ErpStockinventory.class);
		List<ErpStockinventoryentryDto> erpStockinventoryentryDtoList = formDto.getErpStockinventoryentryDto();
		List<ErpStockinventoryentry> erpStockinventoryentryList = BeanUtil.copyList(erpStockinventoryentryDtoList, ErpStockinventoryentry.class);
		String dataId = erpStockinventoryService.addErpStockinventory(erpStockinventoryDto,erpStockinventory,erpStockinventoryentryDtoList,erpStockinventoryentryList);
		if (dataId != null) {
			codeRuleService.useEncode("InStockCode");
		}
		return dataId == null ? Response.notOk() : Response.ok(dataId);
	}

	/**
	 * 修改 
	 */
	@PutMapping("/{id}")
	@ApiOperation(value = "修改", notes = "传入erpStockinventory")
	public Response update(@PathVariable String id, @RequestBody SaveErpStockinventoryFormDataDto formDto) {
		ErpStockinventoryDto erpStockinventoryDto = formDto.getErpStockinventoryDto();
		ErpStockinventory erpStockinventory = BeanUtil.copy(erpStockinventoryDto, ErpStockinventory.class);

		List<ErpStockinventoryentryDto> erpStockinventoryentryDtoList = formDto.getErpStockinventoryentryDto();
		List<ErpStockinventoryentry> erpStockinventoryentryList = BeanUtil.copyList(erpStockinventoryentryDtoList, ErpStockinventoryentry.class);
		return Response.status(erpStockinventoryService.updateErpStockinventory(id, erpStockinventoryDto,erpStockinventory,erpStockinventoryentryDtoList,erpStockinventoryentryList));
	}


	
	/**
	 * 删除 
	 */
	@DeleteMapping("/{ids}")
	@ApiOperation(value = "删除", notes = "传入ids")
	public Response remove(@ApiParam(value = "主键集合", required = true) @PathVariable String ids) {
		return Response.status(erpStockinventoryService.removeByIds(Func.toStrList(ids)));
	}

	
	/**
	 * excel批量导入数据
	 */
	@PostMapping("/import")
	@ApiOperation(value = "excel批量导入数据", notes = "传入菜单id")
	public Response importData(@RequestParam("F_ModuleId") String moduleId, MultipartFile file) {
		XjrExcelImport excelImport = importService.getByModuleId(moduleId);
		ImportDataParser parser = ParserFactory.getImportDataParser();
		File excelFile = IoUtil.toFile(file);
		List<Map<String, Object>> recordList = parser.parseDataToMap(excelImport, excelFile);
		if (CollectionUtil.isNotEmpty(recordList)) {
			List<ErpStockinventoryDto> entityDtoList = recordList.stream().map(record -> {
				try {
					return objectMapper.readValue(objectMapper.writeValueAsString(record), ErpStockinventoryDto.class);
				} catch (JsonProcessingException e) {
				}
				return null;
			}).collect(Collectors.toList());
			List<ErpStockinventory> entities = BeanUtil.copyList(entityDtoList, ErpStockinventory.class);
			return Response.status(erpStockinventoryService.saveBatch(entities));
		}
		return Response.notOk();
	}
}
